package com.projects.android.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.projects.android.R;
import com.projects.android.bus.MenuBUS;
import com.projects.android.dto.FoodDTO;
import com.projects.android.dto.MenuDTO;
import com.projects.android.utils.ImageDownloader;
import com.projects.android.utils.SelectedItems;
import com.projects.android.utils.Utils;
import com.projects.android.views.Cart;
import com.projects.android.views.EditQuantity;
import com.projects.android.views.Menu;

public class SelectedFoodsAdapter extends ArrayAdapter<FoodDTO> {
	private LayoutInflater mInflater;
	private Context ctx;
	private final ImageDownloader imageDownloader = new ImageDownloader();

	public SelectedFoodsAdapter(Context context, int resource,
			int textViewResourceId, List<FoodDTO> objects) {
		super(context, resource, textViewResourceId, objects);
		ctx = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SelectedFoodViewHolder holder = null;
		TextView title = null;
		TextView detail = null;
		final EditText quantity;
		ImageButton remove = null;
		ImageView i11 = null;
		final FoodDTO rowData = getItem(position);

		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.selectedfooditem, null);
			holder = new SelectedFoodViewHolder(convertView);
			convertView.setTag(holder);
		}

		holder = (SelectedFoodViewHolder) convertView.getTag();

		title = holder.gettitle();
		title.setText(rowData.getName());

		detail = holder.getdetail();
		detail.setText("Price:" + String.valueOf(rowData.getPrice()));

		i11 = holder.getImage();
		int startPoint = Utils.SERVICE_URI.indexOf('.');
		int startDeli = Utils.SERVICE_URI.indexOf('/', startPoint);
		String webUrl = Utils.SERVICE_URI.substring(0, startDeli);
		String imgUrl = webUrl + "/Content/Images/" + rowData.getImage();
		Log.i("Image URL", imgUrl);
		imageDownloader.download(imgUrl, i11);

		quantity = holder.getQuantity();
		quantity.setText(String.valueOf(rowData.getQuantity()));
		quantity.setOnClickListener(new EditText.OnClickListener() {
			@Override
			public void onClick(View v) {
				final EditQuantity dlg = new EditQuantity(ctx, quantity.getText().toString());
				dlg.setTitle("Update quantity");
				dlg.show();
				dlg.getBtnOK().setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						rowData.setQuantity(Integer.valueOf(dlg.getTxtQuantity().getText().toString()));
						quantity.setText(String.valueOf(rowData.getQuantity()));
						dlg.dismiss();
					}
				});
			}
		});

		remove = holder.getRemove();
		remove.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(ctx,
						"You have removed '" + rowData.getName() + "'",
						Toast.LENGTH_SHORT).show();
				// Update selected foods
				SelectedItems.SelectedFoods.remove(rowData);
				Cart.CartView.loadData();
				// Update all foods, obly get foods that it is not exist in
				// selected foods list
				SelectedItems.AllFoods.clear();
				ArrayList<MenuDTO> arrMenus = MenuBUS
						.selectByIDRestaurant(SelectedItems.SelectedRestaurant
								.getID());
				for (MenuDTO menu : arrMenus) {
					ArrayList<FoodDTO> foods = menu.getFoods();
					for (FoodDTO food : foods) {
						boolean exist = false;
						for (FoodDTO item : SelectedItems.SelectedFoods) {
							if (item.getID() == food.getID()) {
								exist = true;
								break;
							}
						}
						if (!exist) {
							// Set menu name for each food item
							food.setMenuName(menu.getName());
							SelectedItems.AllFoods.add(food);
						}
					}
				}
				Menu.MenuView.loadData();
				if(SelectedItems.SelectedFoods.size() == 0){
					Cart.btnCheckOut.setEnabled(false);
				}
			}
		});

		return convertView;
	}
}
